//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
import gridHeader from '../planTask/planTaskType'
let extension = {
  components: {//动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: gridHeader,//{ template: "<div>扩展组xx件</div>" },
    gridBody: '',
    gridFooter: '',
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  buttons: { view: [], box: [], detail: [] },//扩展的按钮
  methods: {//事件扩展
    getFormOption (field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        });
      });
      return option;
    },
    onInit () {
      let productidOption = this.getFormOption("productid"); 
      let supplierCode = this.getFormOption("supplierCode"); 
      productidOption.data = [];
      productidOption.disabled = true;
      let producttypeOption = this.getFormOption("producttype");
      producttypeOption.onChange = (val, selectdata) => {
        if (val.length != 0) {
          this.getProduct(val, productidOption);
        }
      };
      supplierCode.onChange = (val, selectdata) => {
        this.getProducttypeList ()
      };
    },
    getGoodList (supplierCode, scenic_poiId) {
      this.http.post("/api/Mall_good/GetProcureGoods", { scenic_poiId, supplierCode }).then((res) => {
        const goods_id = this.getFormOption("goods_id");
        goods_id.data = []
        res.data.map((v) => {
          goods_id.data.push({ key: v.goods_id, value: v.name })
        })
      });
    },
    getscenicPoid (row) {
      this.http.post("/api/Mall_procure_scenic/getPageData",
        { order: "desc", page: 1, rows: 1000, sort: "id", wheres: `[{\"name\":\"scenic_supplierid\",\"value\":\"${row.supplierCode}\",\"displayType\":\"select\"}]` }).then((res) => {
          const scenic_poiId = this.getFormOption("scenic_poiId");
          scenic_poiId.data = []
          res.rows.forEach((v) => {
            scenic_poiId.data.push({ key: v.scenic_poiId, value: v.scenic_name })
            if (row.scenic_poiId == v.scenic_poiId) {
              this.editFormFields.scenic_name = v.scenic_name
            }
          })
        });
    },
    modelOpenBefore (row) {
      let isdisable = this.getFormOption("isdisable");
      isdisable.extra = {}
      if (this.currentAction == "Add") {
      } else {
        this.editFormFields.producttype = Number(row.producttype);
      }
    },
    modelOpenAfter (row) {
      //弹窗打开之后
      let productidOption = this.getFormOption("productid"); 
      if (this.currentAction == "Add") {
      } else {
        this.getProducttypeList() // 回显级联
        // this.getGoodList(row.supplierCode, row.scenic_poiId) // 景区列表获取
        // this.getscenicPoid(row)  // 景区名称回显
        // this.editFormFields.producttype = 363 // 后端返回这个级联得数据 作为回显
        this.getProduct(row.producttype,productidOption) // 回显商品
        this.editFormFields.productid = this.editFormFields.goods_id
        row.intervalTime = row.intervalTime
        this.editFormFields.timeContainer = [row.startTime, row.endTime]
        const isdisable = this.getFormOption("isdisable");
        isdisable.extra = {
          icon: "ios-add",
          text: "设置下单信息",
          hidden: true,
          style: "line-height: 31px;margin-left: 11px;",
          click: (column, item, tableData) => {
            this.$refs.gridHeader.url = ''
            this.$refs.gridHeader.userList = []
            this.$refs.gridHeader.open(row);
          }
        };
      }
    },
    // addAfter(res){
    //   console.log("%c 🚐: addAfter -> res ", "font-size:16px;background-color:#701ad5;color:white;", res)
    //   return false
    // },
    addBefore (formData) {
      //新建保存前formData为对象，包括明细表
      formData.mainData.startTime = this.editFormFields.timeContainer[0]
      formData.mainData.endTime = this.editFormFields.timeContainer[1]
      formData.mainData.status = 1
      if (this.editFormFields.minSeconds >= this.editFormFields.maxSeconds) {
        this.$Message.error("秒数止大于秒数起");
        return false
      } else if (!this.editFormFields.goods_name) {
        this.$Message.error("商品名称不能为空");
        return false
      } 
      // else if (!this.editFormFields.scenic_name) {
      //   this.$Message.error("景区不能为空");
      //   return false
      // } 
      else {
        return formData
      }
    },
    updateBefore (formData) {
      //编辑保存前formData为对象，包括明细表
      formData.mainData.startTime = this.editFormFields.timeContainer[0]
      formData.mainData.endTime = this.editFormFields.timeContainer[1]
      formData.mainData.status = -1
      if (this.editFormFields.minSeconds >= this.editFormFields.maxSeconds) {
        this.$Message.error("秒数止大于秒数起");
        return false
      } else if (!this.editFormFields.goods_name) {
        this.$Message.error("商品名称不能为空");
        return false
      } 
      // else if (!this.editFormFields.scenic_name) {
      //   this.$Message.error("景区不能为空");
      //   return false
      // } 
      else {
        return formData
      }
    },

    // 获取产品名称下拉框数据
    getProduct(val, productidOption) {
      if (!this.editFormFields.phone || !this.editFormFields.reservationTime) {
        return this.$Message.error("请先填写时间和手机号");
      } else if (this.editFormFields.supplierCode != "") {
        this.getShangPin(val, productidOption)
      } else {
        this.$Message.error("请先选择采购供应商");
      }
    },
    getShangPin(val, productidOption) {
      this.http
        .post("/api/Mall_good/GetMallGoods", {
          tpyeid: val,
          distributionid: this.editFormFields.supplierCode,
          date: this.editFormFields.reservationTime,
          phone: this.editFormFields.phone,
        })
        .then(res => {
          productidOption.disabled = false;
          productidOption.data= res.data.map((v) => {
            return {
              key:v.goods_id,
              value:v.name,
              price:v.price,
              goods_id:v.goods_id,
              goodcode:v.goodcode,
              goods_name:v.name
            }
          })
        });
    },
    getProducttypeList () {
      this.http
        .get(
          "api/producttypes/GetDistriProductType?distributorCode=" +
            this.editFormFields.supplierCode
        )
        .then(res => {
          this.getFormOption('producttype').orginData = res.data;
          this.getFormOption('producttype').data = res.data;
        });
    },
  }
};
export default extension;
